﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PLM.ConfigurationCenter.Service;

namespace PLM.ConfigurationCenter.Backup.CustomForms {
    public partial class RecordForm : Form {

        private LogInfoService _recordService = new LogInfoService();
        private string TaskId;

        public RecordForm(string id) {
            InitializeComponent();
            TaskId = id;
        }

        private void RecordForm_Load(object sender, EventArgs e) {
            //加载执行记录列表
            this.lv_log.HeaderStyle = ColumnHeaderStyle.Clickable;
            this.lv_log.View = View.Details;
            this.lv_log.Columns.Add("执行时间", 200, HorizontalAlignment.Center);
            this.lv_log.Columns.Add("服务器地址", 100, HorizontalAlignment.Center);
            this.lv_log.Columns.Add("数据库", 150, HorizontalAlignment.Center);
            this.lv_log.Columns.Add("存储位置", 200, HorizontalAlignment.Center);
            this.lv_log.Columns.Add("状态", 100, HorizontalAlignment.Center);
            this.lv_log.Columns.Add("日志", 440, HorizontalAlignment.Center);
            LoadBackupLogList();
        }
        #region 执行记录
        /// <summary>
        /// 加载执行记录
        /// </summary>
        private void LoadBackupLogList() {
            var logModels = _recordService.CommQuery(p=>p.TaskId == TaskId);

            this.lv_log.Items.Clear();
            //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度  
            this.lv_log.BeginUpdate();
            //添加5行数据  
            foreach (var item in logModels) {
                ListViewItem lvi = new ListViewItem();

                lvi.Text = item.ExecuteTime.ToString("yyyy-MM-dd HH:mm:ss");

                lvi.SubItems.Add(item.DataBase);

                lvi.SubItems.Add(item.ServerAddress);
                lvi.SubItems.Add(item.FilePath);
                lvi.SubItems.Add(item.ExecuteSatus ? "成功" : "失败");
                lvi.SubItems.Add(item.Remark);
                this.lv_log.Items.Add(lvi);
            }

            this.lv_log.EndUpdate();  //结束数据处理，UI界面一次性绘制。  

        }

        #endregion
    }
}
